import math
import random
import lepton
from lepton import Particle, renderer, ParticleGroup
import pyglet
from pyglet.gl import *


class StarField:

	stars_tex = pyglet.resource.texture('stars.png')
	cell_size = stars_tex.width

	def __init__(self, width, height):
		self.group = ParticleGroup(renderer=renderer.BillboardRenderer())
		self.width = width
		self.height = height
		self.create_stars()

	def create_stars(self):
		cells_wide = self.width // self.cell_size + 1
		cells_high = self.height // (3 * self.cell_size // 4) + 1
		offset = 0

		for cx in range(-cells_wide // 2, cells_wide):
			for cy in range(-cells_high // 2, cells_high):
				inflation = random.uniform(1.0, 1.75)
				particle = Particle(
					position=(cx * self.cell_size + offset, cy * self.cell_size * 0.75, 0),
					size=(self.cell_size * inflation, self.cell_size * inflation, 0),
					color=(1,1,1,1),
					up=(0,0,random.uniform(0, 2*math.pi)))
				self.group.new(particle)
				offset = (not offset) * self.cell_size // 2
		self.group.update(0)
	
	def draw(self):
		glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_TEXTURE_BIT)
		glBlendFunc(GL_SRC_ALPHA,GL_ONE)
		glDisable(GL_DEPTH_TEST)
		glDisable(GL_LIGHTING)
		glEnable(self.stars_tex.target)
		glTexParameteri(self.stars_tex.target, GL_TEXTURE_WRAP_S, GL_CLAMP)
		glTexParameteri(self.stars_tex.target, GL_TEXTURE_WRAP_T, GL_CLAMP)
		glTexParameteri(self.stars_tex.target, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
		glTexParameteri(self.stars_tex.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
		glBindTexture(self.stars_tex.target, self.stars_tex.id)
		self.group.draw()
		glPopAttrib()


if __name__ == '__main__':
	win = pyglet.window.Window(width=1024, height=768)
	win.clear()
	glEnable(GL_BLEND)
	glShadeModel(GL_SMOOTH)
	glBlendFunc(GL_SRC_ALPHA,GL_ONE)
	glDisable(GL_DEPTH_TEST)

	stars = StarField()
	scroll_x = scroll_y = scroll_dx = scroll_dy = 0
	SCROLL_KEY_SPEED = 100.0
	
	@win.event
	def on_key_press(symbol, modifiers):
		global scroll_dx, scroll_dy
		if not modifiers:
			if symbol == pyglet.window.key.LEFT:
				scroll_dx = -SCROLL_KEY_SPEED
				return pyglet.event.EVENT_HANDLED
			elif symbol == pyglet.window.key.RIGHT:
				scroll_dx = SCROLL_KEY_SPEED
				return pyglet.event.EVENT_HANDLED
			elif symbol == pyglet.window.key.UP:
				scroll_dy = SCROLL_KEY_SPEED
				return pyglet.event.EVENT_HANDLED
			elif symbol == pyglet.window.key.DOWN:
				scroll_dy = -SCROLL_KEY_SPEED
				return pyglet.event.EVENT_HANDLED
	
	@win.event
	def on_key_release(symbol, modifiers):
		global scroll_dx, scroll_dy
		if symbol in (pyglet.window.key.LEFT, pyglet.window.key.RIGHT):
			scroll_dx = 0
			return pyglet.event.EVENT_HANDLED
		elif symbol in (pyglet.window.key.UP, pyglet.window.key.DOWN):
			scroll_dy = 0
			return pyglet.event.EVENT_HANDLED

	def on_update(dt):
		global scroll_x, scroll_y, scroll_dx, scroll_dy
		scroll_x += scroll_dx * dt
		scroll_y += scroll_dy * dt

	pyglet.clock.schedule_interval(on_update, 1.0/30)

	@win.event
	def on_draw():
		win.clear()
		glLoadIdentity()
		glTranslatef(-scroll_x, -scroll_y, 0)
		stars.draw()
	
	pyglet.app.run()
